package com.zzyl.serve.service.impl;
import java.util.Arrays;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanAll;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        // 1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2.根据护理计划id查询关联的所有护理项目
        nursingPlanVo.setId(id.longValue());
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);

        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        LambdaQueryWrapper<NursingPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.setEntity(nursingPlan);
        return list(wrapper);
    }

    /**
     * 新增护理计划
     * 
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.stream().forEach(projectPlan ->{
            projectPlan.setPlanId(nursingPlan.getId());
        });

        return nursingProjectPlanMapper.intBash(projectPlans);
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //修改计划表
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setId(nursingPlanDto.getId().intValue());
        int count = nursingPlanMapper.updateNursingPlan(nursingPlan);

        //修改关系表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans !=null && projectPlans.size()>0){
            //先删除
            nursingProjectPlanMapper.deleteNursingProject(nursingPlanDto.getId());
            //再新增
            projectPlans.forEach(projectPlan ->
                    projectPlan.setPlanId(nursingPlanDto.getId().intValue()));
            nursingProjectPlanMapper.intBash(projectPlans);
        }

        return count;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1:0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        return removeById(id) ? 1:0;
    }

    @Override
    public List<NursingPlanAll> getPalnAll() {
        return  nursingPlanMapper.getPalnAll();
    }

    @Transactional
    @Override
    public int delById(Integer id) {
        //先删除护理计划
        int count= nursingPlanMapper.deleteById(id);

        //在删除关系表
        nursingProjectPlanMapper.deleteNursingProject(id.longValue());

        return  count;
    }
}